package com.shawn.mobliesafe;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.shawn.bean.BlackNumInfo;
import com.shawn.db.dao.BlackNumDao;
import com.shawn.utils.MyAsycnTaks;

import java.util.List;

public class CallSmsSafeActivity extends AppCompatActivity {

    private ListView lv_callsmssafe_blacknums;
    private ProgressBar loading;
    private BlackNumDao blackNumDao;
    private List<BlackNumInfo> list;
    private MyAdapter myAdapter;
    //查询的总个数
    private final int MAXNUM=20;
    //起始位置
    private int startIndex = 0;

    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_sms_safe);
        lv_callsmssafe_blacknums = (ListView)findViewById(R.id.lv_callsmssafe_blacknums);
        loading = (ProgressBar)findViewById(R.id.loading);
        blackNumDao = new BlackNumDao(getApplicationContext());
        fillData();

        //listview滑动监听事件
        lv_callsmssafe_blacknums.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //当滑动状态改变的时候调用的方法
                //view : listview
                //scrollState : 滑动状态
                //SCROLL_STATE_IDLE : 空闲的状态
                //SCROLL_STATE_TOUCH_SCROLL : 缓慢滑动的状态
                //SCROLL_STATE_FLING : 快速滑动
                if(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    //获取界面显示最后一个条目
                    int position = lv_callsmssafe_blacknums.getLastVisiblePosition();
                    if(position == list.size() - 1) {
                        startIndex += MAXNUM;
                        fillData();
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    private void fillData() {
        new MyAsycnTaks(){

            @Override
            public void preTask() {
                loading.setVisibility(View.VISIBLE);
            }

            @Override
            public void doinBack() {
                if(list == null) {
                    list = blackNumDao.getPartBlackNum(MAXNUM,startIndex);
                }else {
                    list.addAll(blackNumDao.getPartBlackNum(MAXNUM,startIndex));
                }
            }

            @Override
            public void postTask() {
                if(myAdapter == null) {
                    myAdapter = new MyAdapter();
                    lv_callsmssafe_blacknums.setAdapter(myAdapter);
                }else {
                    myAdapter.notifyDataSetChanged();
                }
                loading.setVisibility(View.INVISIBLE);
            }
        }.execute();
    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final BlackNumInfo blackNumInfo = list.get(position);
            ViewHolder viewHolder;
            if(convertView == null) {
                convertView = View.inflate(CallSmsSafeActivity.this,R.layout.item_callsmssafe,null);
                viewHolder = new ViewHolder();
                viewHolder.tv_itemcallsmssafe_blacknum = (TextView) convertView.findViewById(R.id.tv_itemcallsmssafe_blacknum);
                viewHolder.tv_itemcallsmssafe_mode = (TextView) convertView.findViewById(R.id.tv_itemcallsmssafe_mode);
                viewHolder.iv_itemcallsmssafe_delete = (ImageView) convertView.findViewById(R.id.iv_itemcallsmssafe_delete);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv_itemcallsmssafe_blacknum.setText(blackNumInfo.getBlacknum());
            int mode = blackNumInfo.getMode();
            switch (mode) {
                case  BlackNumDao.CALL:
                    viewHolder.tv_itemcallsmssafe_mode.setText("电话拦截");
                    break;
                case BlackNumDao.SMS:
                    viewHolder.tv_itemcallsmssafe_mode.setText("短信拦截");
                    break;
                case BlackNumDao.ALL:
                    viewHolder.tv_itemcallsmssafe_mode.setText("全部拦截");
                    break;
            }
            /**
             * 删除黑名单
             */
            viewHolder.iv_itemcallsmssafe_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(CallSmsSafeActivity.this)
                            .setTitle("提示信息")
                            .setMessage("您确认要删除黑名单号码:\n"+blackNumInfo.getBlacknum()+"吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    blackNumDao.deleteBlackNum(blackNumInfo.getBlacknum());
                                    list.remove(position);
                                    myAdapter.notifyDataSetChanged();
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消",null)
                            .show();
                }
            });
            return convertView;
        }
    }

    /**
     * 存放控件的容器
     */
    static class ViewHolder{
        TextView tv_itemcallsmssafe_blacknum,tv_itemcallsmssafe_mode;
        ImageView iv_itemcallsmssafe_delete;
    }

    public void addBlackNum(View v){
        View view = View.inflate(this,R.layout.dialog_add_blacknum,null);
        dialog = new AlertDialog.Builder(this).create();
        dialog.setCancelable(false);
        dialog.setView(view);
        dialog.show();
        final EditText et_addblacknum_blacknum = (EditText) view.findViewById(R.id.et_addblacknum_blacknum);
        final RadioGroup rg_addblacknum_modes = (RadioGroup) view.findViewById(R.id.rg_addblacknum_modes);
        Button btn_ok = (Button) view.findViewById(R.id.btn_ok);
        Button btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String blacknum = et_addblacknum_blacknum.getText().toString().trim();
                if (TextUtils.isEmpty(blacknum)) {
                    Toast.makeText(getApplicationContext(), "请输入黑名单号码", Toast.LENGTH_SHORT).show();
                    Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
                    et_addblacknum_blacknum.startAnimation(shake);
                    Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                    vibrator.vibrate(100);
                    return;
                }
                int mode = -1;
                int radioButtonId = rg_addblacknum_modes.getCheckedRadioButtonId();
                switch (radioButtonId) {
                    case R.id.rb_addblacknum_tel:
                        mode = BlackNumDao.CALL;
                        break;
                    case R.id.rb_addblacknum_sms:
                        mode = BlackNumDao.SMS;
                        break;
                    case R.id.rb_addblacknum_all:
                        mode = BlackNumDao.ALL;
                        break;
                }
                blackNumDao.addBlackNum(blacknum,mode);
                list.add(0, new BlackNumInfo(blacknum, mode));
                myAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });
        btn_cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //隐藏对话框
                dialog.dismiss();
            }
        });
    }
}
